import baseComponents from './baseComponents.js'
import {deepClone, assign} from '../../../utils/index.js'
let idGlobal = 1000

export function createIdAndKey(item) {
    const config = item.__config__
    config.formId = ++idGlobal
    config.renderKey = `${config.formId}${+new Date()}` // 改变renderKey后可以实现强制更新组件
    if (config.layout === 'colFormItem') {
      item.__vModel__ = `field${idGlobal}`
    } else if (config.layout === 'rowFormItem') {
      config.componentName = `row${idGlobal}`
      !Array.isArray(config.children) && (config.children = [])
      delete config.label // rowFormItem无需配置label属性
    }
    if (Array.isArray(config.children)) {
      config.children = config.children.map(childItem => createIdAndKey(childItem))
    }
    return item
}

export function getInput(source){
    let target = deepClone(baseComponents.find(t=>t.__config__.label==='单行文本'))
    assign(target,{
      __config__: {
        span:12,
        showUpBtn: false, 
        showDownBtn: false, 
        showDeleteButton: false,
        canDraggable: false
      }
    })
    assign(target,source)
    return target
}

export function getSelect(source){
    let target = deepClone(baseComponents.find(t=>t.__config__.label==='下拉列表'))
    assign(target,{
      __config__: {
        span:12,
        showUpBtn: false, 
        showDownBtn: false, 
        showDeleteButton: false,
        showOptionChange: false,
        canDraggable: false
      }
    })
    if(target.__slot__.options) target.__slot__.options = []
    assign(target,source)
    return target
  }